<?
global $frm, $smarty, $settings;

$payment_filter = '';
if ($frm['pfilter']) {
	$payments = preg_split ('/\\s*,\\s*/', $settings['payments']);
	if (in_array ($frm['pfilter'], $payments)) {
		$payment_filter = ' and pay_systems like \'%' . $frm['pfilter'] . '%\' ';
	}
}

$listings = array ();
$q = 'select id from hl_groups where status = 1';
$sth = mysql_query ($q);
while ($row = mysql_fetch_array ($sth)) {
	array_push ($listings, $row['id']);
}

$groups_where = '-1';
if ($listings) {
	$groups_where = join (',', $listings);
}

$listings = array ();
$q = 'select
                hl_listings.*,
                hl_groups.type,
                date_format(date_added, \'%b %D, %Y\') as added,
                date_format(date_closed, \'%b %D, %Y\') as closed,
                (to_days(now()) - to_days(date_added)) as monitored,
                (date_added + interval ' . $settings['new_for_days'] . ('' . ' day > current_date) as new
         from
                hl_listings inner join hl_groups
                 on hl_listings.group_id = hl_groups.id
         where 
                group_id in (' . $groups_where . ') and 
                (hl_listings.name like \'%' . $frm['content'] . '%\')
                ' . $payment_filter . ' and 
                (hl_listings.expiration = 0 || date_added + interval hl_listings.expiration day >= current_date) and
                hl_listings.status = 1
         order by 
                date_added asc');

($lsth = mysql_query ($q) OR print mysql_error ());
$smarty->assign ('q', $q);
$last_date = '';
while ($lrow = mysql_fetch_array ($lsth)) {
	if ($last_date != $lrow['added']) {
		array_push ($listings, array ('data_type' => 'date', 'date' => $lrow['added']));
		$last_date = $lrow['added'];
	}

	$lrow['data_type'] = 'listing';
	$lrow = get_listing_details ($lrow);
	array_push ($listings, $lrow);
}

$smarty->assign ( 'premiumGroupId', $premiumGroupId );
$smarty->assign ( 'scamGroupId', $scamGroupId );
$smarty->assign ( 'closeGroupId', $closeGroupId );
$smarty->assign ('listings', $listings);
$smarty->display ('search.tpl');





?>
